/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpDict;
import com.jims.master.modules.csm.entity.ExpNameDict;
import com.jims.master.modules.csm.service.ExpDictService;
import com.jims.master.modules.csm.service.ExpNameDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 消耗品字典维护Controller
 * @author zhuq
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expDict")
public class ExpDictController extends BaseController {

    @Autowired
    private ExpDictService expDictService;
    @Autowired
    ExpNameDictService expNameDictService;

    @ModelAttribute
    public ExpDict get(@RequestParam(required=false) String id) {
        ExpDict entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = expDictService.get(id);
        }
        if (entity == null){
            entity = new ExpDict();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ExpDict expDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpDict> page = expDictService.findPage(new Page<ExpDict>(request, response), expDict);
        model.addAttribute("page", page);
        return "modules/csm/expDictIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String findlist(ExpDict expDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpDict> page = expDictService.findPage(new Page<ExpDict>(request, response), expDict);
        model.addAttribute("page", page);
        return "modules/csm/expDictQuery";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"queryList", ""})
    public String findExpCode(ExpDict expDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<ExpDict> expDictList = expDictService.findByExpCode(expDict);
        model.addAttribute("expDictList", expDictList);
        model.addAttribute("expDict", expDict);
        return "modules/csm/expDictQuery";
    }

    /**
     * 根据消耗品expCode查询列表
     * @param expDict
     * @author  zhuq
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"dialog", ""})
    public String findByExpCode(ExpDict expDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<ExpDict> expDictList = expDictService.findByExpCode(expDict);
        model.addAttribute("expDictList", expDictList);
        model.addAttribute("expDict", expDict);
        return "modules/csm/expDictIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ExpDict expDict, Model model) {
        //根据expCode获取expName
        ExpNameDict expNameDict=new ExpNameDict();
        expNameDict=expNameDictService.findByExpCode(expDict.getExpCode());
        expDict.setExpName(expNameDict.getExpName());
        model.addAttribute("expDict", expDict);
        return "modules/csm/expDictForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ExpDict expDict, Model model) {
        StringData data=new StringData();
        expDict.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(expDict.getExpName()));
        int i=expDictService.save(expDict);
        if(i>0){
            data.setCode("success");
            data.setData("保存消耗品成功");
        }else{
            data.setCode("error");
            data.setData("保存消耗品失败");
        }
        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ExpDict expDict) {
        StringData data=new StringData();
        int i=expDictService.delete(expDict);
        if(i>0){
            data.setCode("success");
            data.setData("删除消耗品成功");
        }else{
            data.setCode("error");
            data.setData("删除消耗品失败");
        }
        return data;
    }


    /**
     * @param expDict 传递参数
     * @return List<ExpDict>    返回类型
     * @throws
     * @Title: autoComplete
     * @Desciption: (消耗品基本信息添加消耗品字典自动补全)
     * @author CTQ
     * @date 2017-03-02
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoComplete", ""})
    @ResponseBody
    public List<ExpDict> autoComplete(String q,String limit,ExpDict expDict) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<ExpDict> list = expDictService.findAutoCompleteList(expDict,q,limit);
        return list;
    }

}
